package com.yjzx.util.common.util.file;

import java.io.*;

public class ExternalClassLoader extends ClassLoader {
    private String classPath;

    public ExternalClassLoader(String classPath) {
        this.classPath = classPath;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String path = name.replace('.', '/') + ".class";
        File file = new File(classPath + File.separator + path);

        if (!file.exists()) {
            throw new ClassNotFoundException("Class not found: " + name);
        }
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] classData = FileUtils.readFully(fis);
            return defineClass(name, classData, 0, classData.length);
        } catch (IOException e) {
            throw new ClassNotFoundException("Failed to load class: " + name, e);
        }
    }
}
